<template>
  <div>
    <!--:model="shop" 数据双向绑定-->
    <!--ref="shopForm" id="shopForm",给form去一个名字-->
    <!--:rules="formRules" 校验规则-->
    <el-form :model="shopDTO" ref="shopForm" :rules="formRules" label-position="left" label-width="100px"
             class="demo-ruleForm login-container">
      <h3 class="title">店铺入驻</h3>
      <el-form-item prop="shop.name" label="名称">
        <el-input type="text" v-model="shopDTO.shop.name" auto-complete="off" placeholder="请输入店铺名称！"></el-input>
      </el-form-item>
      <el-form-item prop="tel" label="座机">
        <el-input type="text" v-model="shopDTO.shop.tel" auto-complete="off" placeholder="请输入座机！"></el-input>
      </el-form-item>
      <el-form-item prop="address" label="店铺地址">
        <el-input type="text" v-model="shopDTO.shop.address" auto-complete="off" placeholder="请输入地址！"></el-input>
        <!--        <el-button size="small" type="primary" @click="selectAdrress">选择</el-button>-->
      </el-form-item>
      <el-form-item prop="logo" label="店铺Logo">
        <el-upload
            class="upload-demo"
            action="http://localhost:80/file/fileUpload"
            name="fileName"
            :on-success="handleSuccess"
            :on-preview="handlePreview"
            :on-remove="handleRemove"
            :file-list="fileList"
            :before-upload="beforeAvatarUpload"
            :limit=1
            accept=".jpg,.png"
            list-type="picture">
          <el-button size="small" type="primary">点击上传</el-button>
          <div slot="tip" class="el-upload__tip">只能上传jpg/png文件，且不超过1MB</div>
        </el-upload>
      </el-form-item>
      <h3 class="title">管理员信息设置</h3>
      <el-form-item prop="username" label="账号">
        <el-input type="text" v-model="shopDTO.employee.username" auto-complete="off"
                  placeholder="请输入账号！"></el-input>
      </el-form-item>
      <el-form-item prop="phone" label="手机号码">
        <el-input type="text" v-model="shopDTO.employee.phone" auto-complete="off" placeholder="请输入电话！"></el-input>
      </el-form-item>
      <el-form-item prop="email" label="电子邮件">
        <el-input type="text" v-model="shopDTO.employee.email" auto-complete="off" placeholder="请输入邮件！"></el-input>
      </el-form-item>
      <el-form-item prop="password" label="密码">
        <el-input type="password" v-model="shopDTO.employee.password" auto-complete="off"
                  placeholder="请输入密码！"></el-input>
      </el-form-item>
      <el-form-item prop="comfirmPassword" label="确认密码">
        <el-input type="password" v-model="shopDTO.employee.comfirmPassword" auto-complete="off"
                  placeholder="请输入确认密码！"></el-input>
      </el-form-item>
      <el-form-item style="width:100%;">
        <el-button type="primary" style="width:100%;" @click.native.prevent="settledIn">入驻</el-button>
      </el-form-item>
    </el-form>
    <el-dialog
        title="选择地址"
        :visible.sync="dialogVisable"
        width="30%">
      <!--<baidu-map :center="{lng: 116.403765, lat: 39.914850}" :zoom="11">
          <bm-view class="map"></bm-view>
          <bm-control :offset="{width: '10px', height: '10px'}">
              &lt;!&ndash;:sugStyle="{zIndex: 2100} 让搜索提示上浮&ndash;&gt;
              <bm-auto-complete v-model="keyword" :sugStyle="{zIndex: 2100}">
                  <div style="margin-bottom:10px">
                      <input id="searchInput" type="text" placeholder="请输入关键字" class="searchinput"/>
                      <el-button type="success" @click="selectAdrressConfirm">确定</el-button>
                  </div>
              </bm-auto-complete>
          </bm-control>
          <bm-local-search :keyword="keyword" :auto-viewport="true" ></bm-local-search>
      </baidu-map>-->
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisable=false">取 消</el-button>
        <el-button type="primary" @click="dialogVisable=true">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
export default {
  data() {

    return {
      dialogVisable: false,
      fileList: [],
      beforeAvatarUpload: "",
      shopDTO: {
        employee: {},
        shop: {}
      }, //这个参数用于保存需要提交到后台的所有参数信息
      formRules: {
        //required:设置当前属性是否必填;message:如果不满足条件,将提示的文本信息;trigger:触发的事件:blur:失去焦点
        'shop.name': [
          {required: true, message: '请输入店铺名称', trigger: 'blur'}
        ]
      }
    }
  },
  methods: {
    selectAdrressConfirm() {
      //获取值搜索框值,设置给地址
      var searchInputV = document.getElementById("searchInput").value;
      this.shop.address = searchInputV;
      //关闭对话框
      this.dialogVisable = false;
    },
    selectAdrress() {
      this.dialogVisable = true;
    },
    //上传文件之前做校验
    /*    beforeAvatarUpload(picture){
          const isJPG = picture.type === 'image/jpeg';
          const isLt2M = picture.size / 1024 / 1024 < 2;

          if (!isJPG) {
            this.$message.error('上传头像图片只能是 JPG 格式!');
          }
          if (!isLt2M) {
            this.$message.error('上传头像图片大小不能超过 1MB!');
          }
          return isJPG && isLt2M;
        },*/
    //文件上传成功回调
    handleSuccess(response, file, fileList) {
      this.shopDTO.shop.logo = response.data;
    },
    //文件上传失败回调
    handlerError(err, file, fileList) {
      console.log(err);
    },
    //文件删除
    handleRemove(file, fileList) {
      var filePath = file.response.resultObj;
      this.$http.delete("/file/fileDel", quanfile)
          .then(res => {
            if (res.data.success) {
              this.$message({
                message: '删除成功!',
                type: 'success'
              });
            } else {
              this.$message({
                message: '删除失败!',
                type: 'error'
              });
            }
          })
    },
    //图片预览
    handlePreview(file) {
      console.log(file);
    },
    //提交入驻
    settledIn() {
      this.$http.post("/shop/register", this.shopDTO).then(res => {
        if (res.data.success) {
          this.$message({
            message: '操作成功!',
            type: 'success'
          });
          //重置表单
          this.$refs['shopForm'].resetFields();
          //跳转登录页面
          this.$router.push({path: '/login'});
        } else {
          this.$message({
            message: res.data.message,
            type: 'error'
          });
        }
      });
    }
  }
}

</script>
<style lang="scss" scoped>

.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}

.avatar-uploader .el-upload:hover {
  border-color: #409EFF;
}

.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  line-height: 178px;
  text-align: center;
}

.avatar {
  width: 178px;
  height: 178px;
  display: block;
}

.login-container {
  -webkit-border-radius: 5px;
  border-radius: 5px;
  -moz-border-radius: 5px;
  background-clip: padding-box;
  margin: 180px auto;
  width: 500px;
  padding: 35px 35px 15px 35px;
  background: #fff;
  border: 1px solid #eaeaea;
  box-shadow: 0 0 25px #cac6c6;

  .title {
    margin: 0px auto 40px auto;
    text-align: center;
    color: #505458;
  }

  .remember {
    margin: 0px 0px 35px 0px;
  }
}

.map {
  width: 100%;
  height: 300px;
}

.searchinput {
  width: 300px;
  box-sizing: border-box;
  padding: 9px;
  border: 1px solid #dddee1;
  line-height: 20px;
  font-size: 16px;
  height: 38px;
  color: #333;
  position: relative;
  border-radius: 4px;
}
</style>
<!--
<template>
  <div>
    &lt;!&ndash;:model="shop" 数据双向绑定&ndash;&gt;
    &lt;!&ndash;ref="shopForm" id="shopForm",给form去一个名字&ndash;&gt;
    &lt;!&ndash;:rules="formRules" 校验规则&ndash;&gt;
    <el-form :model="shopDTO" ref="shopForm" :rules="formRules" label-position="left" label-width="100px"
             class="demo-ruleForm login-container">
      <h3 class="title">店铺入驻</h3>
      <el-form-item prop="shop.name" label="名称">
        <el-input type="text" v-model="shopDTO.shop.name" auto-complete="off" placeholder="请输入店铺名称！"></el-input>
      </el-form-item>
      <el-form-item prop="tel" label="座机">
        <el-input type="text" v-model="shopDTO.shop.tel" auto-complete="off" placeholder="请输入座机！"></el-input>
      </el-form-item>
      <el-form-item prop="address" label="店铺地址">
        <el-input type="text" v-model="shopDTO.shop.address" auto-complete="off" placeholder="请输入地址！"></el-input>
        &lt;!&ndash;        <el-button size="small" type="primary" @click="selectAdrress">选择</el-button>&ndash;&gt;
      </el-form-item>
      <el-form-item prop="logo" label="店铺Logo">
        <el-upload
            name="file1"
            class="avatar-uploader"
            action="http://localhost:80/file/upload"
            :show-file-list="false"
            :on-success="handleAvatarSuccess"
            :before-upload="beforeAvatarUpload">
          <img v-if="imageUrl" :src="imageUrl" class="avatar">
          <i v-else class="el-icon-plus avatar-uploader-icon"></i>
        </el-upload>
      </el-form-item>
      <h3 class="title">管理员信息设置</h3>
      <el-form-item prop="username" label="账号">
        <el-input type="text" v-model="shopDTO.employee.username" auto-complete="off"
                  placeholder="请输入账号！"></el-input>
      </el-form-item>
      <el-form-item prop="phone" label="手机号码">
        <el-input type="text" v-model="shopDTO.employee.phone" auto-complete="off" placeholder="请输入电话！"></el-input>
      </el-form-item>
      <el-form-item prop="email" label="电子邮件">
        <el-input type="text" v-model="shopDTO.employee.email" auto-complete="off" placeholder="请输入邮件！"></el-input>
      </el-form-item>
      <el-form-item prop="password" label="密码">
        <el-input type="password" v-model="shopDTO.employee.password" auto-complete="off"
                  placeholder="请输入密码！"></el-input>
      </el-form-item>
      <el-form-item prop="comfirmPassword" label="确认密码">
        <el-input type="password" v-model="shopDTO.employee.comfirmPassword" auto-complete="off"
                  placeholder="请输入确认密码！"></el-input>
      </el-form-item>
      <el-form-item style="width:100%;">
        <el-button type="primary" style="width:100%;" @click.native.prevent="onSubmit">入驻</el-button>
      </el-form-item>
    </el-form>
    <el-dialog
        title="选择地址"
        :visible.sync="dialogVisable"
        width="30%">
      &lt;!&ndash;<baidu-map :center="{lng: 116.403765, lat: 39.914850}" :zoom="11">
          <bm-view class="map"></bm-view>
          <bm-control :offset="{width: '10px', height: '10px'}">
              &lt;!&ndash;:sugStyle="{zIndex: 2100} 让搜索提示上浮&ndash;&gt;
              <bm-auto-complete v-model="keyword" :sugStyle="{zIndex: 2100}">
                  <div style="margin-bottom:10px">
                      <input id="searchInput" type="text" placeholder="请输入关键字" class="searchinput"/>
                      <el-button type="success" @click="selectAdrressConfirm">确定</el-button>
                  </div>
              </bm-auto-complete>
          </bm-control>
          <bm-local-search :keyword="keyword" :auto-viewport="true" ></bm-local-search>
      </baidu-map>&ndash;&gt;
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisable=false">取 消</el-button>
        <el-button type="primary" @click="dialogFormVisible=true">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
export default {
  data() {
    return {
      imageUrl: '',
      dialogVisable: false,
      shopDTO: {
        employee: {},
        shop: {}
      }, //这个参数用于保存需要提交到后台的所有参数信息
      formRules: {
        //required:设置当前属性是否必填;message:如果不满足条件,将提示的文本信息;trigger:触发的事件:blur:失去焦点
        'shop.name': [
          {required: true, message: '请输入店铺名称', trigger: 'blur'}
        ]
      }
    }
  },
  methods: {
    handleAvatarSuccess(res, file) {//res就是上传成功后，后台返回的结果
      console.log(JSON.stringify(res));
      this.imageUrl = res;
      //将图片的真实地址赋值给shop实体类中的logo属性
      this.shop.shop.logo = res;
    },
    // 这个方法是在：用户选择了上传的文件后&执行上传操作之前   两者之间触发
    beforeAvatarUpload(file) {//该方法主要用于：判断选择上传的文件格式&大小 ，是否满足要求
      const isJPG = file.type === 'image/jpeg';
      const isLt2M = file.size / 1024 / 1024 < 2;

      if (!isJPG) {
        this.$message.error('上传头像图片只能是 JPG 格式!');
      }
      if (!isLt2M) {
        this.$message.error('上传头像图片大小不能超过 2MB!');
      }
      return isJPG && isLt2M;
    },

    onSubmit() {
      console.log(">>>>>"+JSON.stringify(this.shopDTO))
      //发送请求
      this.$http.post("/shop/register", this.shopDTO).then(res => {
        if (res.data.success){
          //直接跳转到首页
          this.$router.push('/login');
        }else {
          this.$message.error(res.data.message);
        }
      })
    }
  },
  mounted() {

  }
}

</script>
<style lang="scss" scoped>

.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.avatar-uploader .el-upload:hover {
  border-color: #409EFF;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  line-height: 178px;
  text-align: center;
}
.avatar {
  width: 178px;
  height: 178px;
  display: block;
}

.login-container {
  -webkit-border-radius: 5px;
  border-radius: 5px;
  -moz-border-radius: 5px;
  background-clip: padding-box;
  margin: 180px auto;
  width: 500px;
  padding: 35px 35px 15px 35px;
  background: #fff;
  border: 1px solid #eaeaea;
  box-shadow: 0 0 25px #cac6c6;

  .title {
    margin: 0px auto 40px auto;
    text-align: center;
    color: #505458;
  }

  .remember {
    margin: 0px 0px 35px 0px;
  }
}

.map {
  width: 100%;
  height: 300px;
}

.searchinput {
  width: 300px;
  box-sizing: border-box;
  padding: 9px;
  border: 1px solid #dddee1;
  line-height: 20px;
  font-size: 16px;
  height: 38px;
  color: #333;
  position: relative;
  border-radius: 4px;
}
</style>
-->
